package com.stu.covid.business.service;

import com.stu.covid.business.constant.Constants;
import com.stu.covid.business.entity.Student;
import com.stu.covid.business.entity.User;
import com.stu.covid.business.manager.SystemManager;
import com.stu.covid.business.mapper.StudentMapper;
import com.stu.covid.business.mapper.UserMapper;
import com.stu.covid.business.utils.*;
import com.stu.covid.config.redis.RedisCloudHandle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Slf4j
public class StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private SystemManager systemManager;

    @Autowired
    private UserMapper userMapper;


    public Result<Student> studentList(Student student) {
        List<Student> list = studentMapper.list(student);
        Integer count = studentMapper.count(student);
        return ResultFactory.bulid(CodeMsg.SUCCESS, count, list);
    }

    @Transactional
    public void studentAdd(Student student) {

        String studentNo = systemManager.getIncreaseNumber(student.getEnrolmentDate(), "student", 1, 4);

        User user = new User();
        user.setRole("student");
        user.setUsername(studentNo);
        user.setPassword(PasswordUtils.genPassword("123456"));
        user.setNickName(student.getName());
        user.setStatus(1);
        user.setIsDelete(0);
        userMapper.insertSelective(user);

        student.setUserId(user.getId());
        student.setStudentNo(studentNo);
        int i = studentMapper.insertSelective(student);


    }

    public void studentUpdate(Student student) {
        studentMapper.updateByPrimaryKeySelective(student);
    }

    public void studentDel(Integer id) {
        studentMapper.deleteByPrimaryKey(id);
    }
}
